package com.yuelife.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuelife.service.dao.SCirclesMapper;
import com.yuelife.service.pojo.SCircles;
import com.yuelife.service.service.SCirclesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:SCircles业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SCirclesServiceImpl implements SCirclesService {

    @Autowired
    private SCirclesMapper sCirclesMapper;


    /**
     * SCircles条件+分页查询
     * @param sCircles 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<SCircles> findPage(SCircles sCircles, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(sCircles);
        //执行搜索
        return new PageInfo<SCircles>(sCirclesMapper.selectByExample(example));
    }

    /**
     * SCircles分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<SCircles> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<SCircles>(sCirclesMapper.selectAll());
    }

    /**
     * SCircles条件查询
     * @param sCircles
     * @return
     */
    @Override
    public List<SCircles> findList(SCircles sCircles){
        //构建查询条件
        Example example = createExample(sCircles);
        //根据构建的条件查询数据
        return sCirclesMapper.selectByExample(example);
    }


    /**
     * SCircles构建查询对象
     * @param sCircles
     * @return
     */
    public Example createExample(SCircles sCircles){
        Example example=new Example(SCircles.class);
        Example.Criteria criteria = example.createCriteria();
        if(sCircles!=null){
            // 
            if(!StringUtils.isEmpty(sCircles.getId())){
                    criteria.andEqualTo("id",sCircles.getId());
            }
            // 
            if(!StringUtils.isEmpty(sCircles.getProvinceName())){
                    criteria.andEqualTo("provinceName",sCircles.getProvinceName());
            }
            // 
            if(!StringUtils.isEmpty(sCircles.getCityName())){
                    criteria.andEqualTo("cityName",sCircles.getCityName());
            }
            // 
            if(!StringUtils.isEmpty(sCircles.getAreaName())){
                    criteria.andEqualTo("areaName",sCircles.getAreaName());
            }
            // 
            if(!StringUtils.isEmpty(sCircles.getName())){
                    criteria.andLike("name","%"+sCircles.getName()+"%");
            }
            // 经度
            if(!StringUtils.isEmpty(sCircles.getLon())){
                    criteria.andEqualTo("lon",sCircles.getLon());
            }
            // 纬度
            if(!StringUtils.isEmpty(sCircles.getLat())){
                    criteria.andEqualTo("lat",sCircles.getLat());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        sCirclesMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改SCircles
     * @param sCircles
     */
    @Override
    public void update(SCircles sCircles){
        sCirclesMapper.updateByPrimaryKey(sCircles);
    }

    /**
     * 增加SCircles
     * @param sCircles
     */
    @Override
    public void add(SCircles sCircles){
        sCirclesMapper.insert(sCircles);
    }

    /**
     * 根据ID查询SCircles
     * @param id
     * @return
     */
    @Override
    public SCircles findById(Integer id){
        return  sCirclesMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询SCircles全部数据
     * @return
     */
    @Override
    public List<SCircles> findAll() {
        return sCirclesMapper.selectAll();
    }
}
